package com.gxa.controller;

import com.gxa.common.LayR;
import com.gxa.common.R;
import com.gxa.param.MyTableParam;
import com.gxa.pojo.Dept;
import com.gxa.pojo.Role;
import com.gxa.service.deptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author xiejinfeng
 * @create 2021-06-10 11:31
 * @descrip:
 */

@Controller
@RequestMapping("/dept")
public class deptController {

    @Autowired
    private deptService service;


    @PostMapping("/list/data")
    @ResponseBody
    public LayR getAll(){
        return service.findAll();
    }

    @PostMapping("/list/Pagedata")
    @ResponseBody
    public LayR list1(MyTableParam param){
        return service.list(param);
    }

    @GetMapping("/list/page")
    public String listPage(){
        return "dept/dept-list";
    }

    @PostMapping("/deleteOne")
    @ResponseBody
    public R deleteOne(Integer deptId){

        return service.deleteOne(deptId);

    }

    @PostMapping("/delete")
    @ResponseBody
    public R delete(String[] deptIds){
        return service.deleteAll(deptIds);
    }

    @GetMapping("/add/page")
    public String addPage(){
        return "dept/dept-add";
    }

    @PostMapping("/add/do")
    @ResponseBody
    public R add(Dept dept){
        return service.add(dept);
    }

    @GetMapping("/edit/page")
    public String editPage(Integer id, Model model, HttpSession session){
        session.setAttribute("editDeptId",id);
        model.addAttribute("editDeptId", id);
        return "dept/dept-edit";
    }

    @PostMapping("/detail")
    @ResponseBody
    public R detail(Integer id){
        return service.findById(id);
    }

    @PostMapping("/edit/do")
    @ResponseBody
    public R edit(Dept dept, HttpSession session){
        Integer editDeptId = (Integer) session.getAttribute("editDeptId");
        dept.setDeptId(editDeptId);
        return service.edit(dept);
    }

}
